<template>
  <div class="mod-config">
      <el-form :inline="true" v-show="searchShow" ref="searchForm" :model="dataForm" @keyup.enter.native="searchHandle">
        <el-form-item label="单据编号" prop="billId">
          <el-input v-model="dataForm.billId" placeholder="单据编号"></el-input>
        </el-form-item>
        <el-form-item label="单据日期">
          <el-row>
            <el-col :span="11">
              <el-date-picker
                v-model="dataForm.createTimeStart"
                style="width: 100%;"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-col>
            <el-col class="line" :span="2" style="text-align: center;">-</el-col>
            <el-col :span="11">
              <el-date-picker
                v-model="dataForm.createTimeEnd"
                style="width: 100%;"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="制单人名称" prop="createUserName">
          <el-input v-model="dataForm.createUserName" placeholder="制单人名称"></el-input>
        </el-form-item>
        <el-form-item label="审核人名称" prop="auditUserName">
          <el-input v-model="dataForm.auditUserName" placeholder="审核人名称"></el-input>
        </el-form-item>
        <el-form-item label="生效日期">
          <el-row>
            <el-col :span="11">
              <el-date-picker
                v-model="dataForm.effectiveTimeStart"
                style="width: 100%;"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-col>
            <el-col class="line" :span="2" style="text-align: center;">-</el-col>
            <el-col :span="11">
              <el-date-picker
                v-model="dataForm.effectiveTimeEnd"
                style="width: 100%;"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select v-model="dataForm.adStatus">
            <el-option value="0" label="未审核"></el-option>
            <el-option value="1" label="已审核"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生效状态">
          <el-select v-model="dataForm.effectStatus">
            <el-option value="0" label="未生效"></el-option>
            <el-option value="1" label="已生效"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
          <el-button type="default" icon="el-icon-delete" @click="searchReset">清空</el-button>
        </el-form-item>

      </el-form>
      <el-row>
        <el-col :span="24">
          <el-button v-if="permissions.eims_adprice_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">按商品新增</el-button>
          <el-button v-if="permissions.eims_adprice_add" icon="el-icon-plus" type="primary" @click="addOrUpdateByBrand()">按品种新增</el-button>
          <el-button icon="el-icon-search" circle @click="handleSearchShow" style="float:right;"></el-button>
          <el-button icon="el-icon-refresh" circle @click="refreshChange" style="float:right;"></el-button>
        </el-col>
      </el-row>
      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight">
          <el-table-column prop="billId" label="单据编号"  width="166"></el-table-column>
          <el-table-column prop="createUserName" label="制单人"></el-table-column>
          <el-table-column prop="createTime" label="单据日期" width="166"></el-table-column>
          <el-table-column prop="auditUserName" label="审核人"></el-table-column>
          <el-table-column prop="adStatus" label="生效状态" :formatter="adStatusFormatter"></el-table-column>
          <el-table-column prop="effectiveTime" label="生效时间" width="166"></el-table-column>
          <el-table-column prop="priceGroupName" label="零售价格种类"></el-table-column>
          <el-table-column prop="memo" label="备注"></el-table-column>
          <el-table-column label="操作" width="166" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-edit" @click="showDetails(scope.row.id,scope.row.adType)">详情</el-button>
              <el-button v-if="permissions.eims_adprice_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id,scope.row.adType)" :disabled="scope.row.adStatus == '1'">修改</el-button>
              <el-button v-if="permissions.eims_adprice_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)"  :disabled="scope.row.adStatus == '1'">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
        </el-pagination>
      </div>
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
      <brand-form v-if="addByBrandVisible" ref="addByBrandDialog" @refreshDataList="getDataList"></brand-form>
      <detail-dialog v-if="detailVisible" ref="detailDialog"></detail-dialog>
      <brand-detail v-if="brandDetailVisible" ref="brandDetail"></brand-detail>

  </div>
</template>

<script>
  import { fetchList, delObj } from "@/api/eims/retail-price-adjust"
  import TableForm from "./form"
  import BrandForm from './brand-form'
  import DetailDialog from "./detail"
  import BrandDetail from "./brandDetail"
  import { mapGetters } from "vuex"
  import { filterParams } from '@/util/tools'

  export default {
    data() {
      return {
        dataForm: {
          createUserName: '',
          auditUserName: '',
          goodsName: ''
        },
        dataList: [],
        searchShow: true,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        addByBrandVisible: false,
        detailVisible: false,
        brandDetailVisible: false
      };
    },
    components: {
      TableForm,
      BrandForm,
      DetailDialog,
      BrandDetail
    },
    created() {
      this.getDataList();
    },
    computed: {
      ...mapGetters(["permissions"]),
      tableHeight: function(){
        return window.innerHeight - 320
      }
    },
    methods: {
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        let params1 = { current: this.pageIndex, size: this.pageSize }
        let params2 = filterParams(this.dataForm)
        fetchList(Object.assign(params1,params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
					this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(id,type) {
        if(type=="2"){
          this.addByBrandVisible = true
          this.$nextTick(() => {
            this.$refs.addByBrandDialog.init(id)
          })
        }else{
          this.addOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.addOrUpdate.init(id)
          })
        }
      },
      // 按品种调整商品零售价
      addOrUpdateByBrand(id){
        this.addByBrandVisible = true
        this.$nextTick(() => {
          this.$refs.addByBrandDialog.init(id)
        })
      },
      // 详情
      showDetails(id,type) {
        if(type == "1"){
          this.detailVisible = true
          this.$nextTick(() => {
            this.$refs.detailDialog.init(id)
          })
        }else{
          this.brandDetailVisible =true
          this.$nextTick(() => {
            this.$refs.brandDetail.init(id)
          })
        }
      },
      // 删除
      deleteHandle(id) {
        this.$confirm("是否确认删除ID为" + id, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delObj(id)
        }).then(data => {
          if(response.data.code == '0'){
            this.$message.success("删除成功")
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      searchChange() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 搜索清空
      searchReset() {
        for(let key in this.dataForm){
          this.dataForm[key] = ''
        }
        this.getDataList(this.page)
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow
      },
      // 刷新回调
      refreshChange() {
        this.getDataList(this.page);
      },
      //格式化表格中的审核状态
      adStatusFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case '0':
            return '未审核'
            break
          case '1':
            return '已生效'
            break
        }
      },
    }
  };
</script>
